/*通过mutation间接更新state*/

import {RECEIVE_SHOPS,RECEIVE_CATEGORIES,RECEIVE_ADDRESS,RECEIVE_USER_INFO,RESET_USER_INFO} from './mutation-types'
import {reqAddress,reqFoodCategorys,reqShops,reqUserInfo,reqLogout} from  '../api'
export default {
  //异步获取
  //地址
  async getAddress({commit,state}){
    //发送请求
  const geohash = state.latitude +','+state.longitude
  const res = await reqAddress(geohash)
    if(res.code == 0){
      const address = res.data

      commit(RECEIVE_ADDRESS,{address})
    }
    //提交一个mutation
  },
  //食品分类
  async getFoodCategorys({commit}){
    //发送请求
    const res = await reqFoodCategorys()
    if(res.code == 0){
      const foodslist = res.data

      commit(RECEIVE_CATEGORIES,foodslist)
    }
    //提交一个mutation
  },
  //商家列表
  async getShops({commit,state}){
    //发送请求
    const {latitude,longitude} = state //latitude = state.latitude
    const res = await reqShops(latitude,longitude)
    if(res.code == 0){
      const shoplist = res.data
      commit(RECEIVE_SHOPS,shoplist)
    }
    //提交一个mutation
  },
  //因为已经有userInfo了，所以要同步记录用户信息
  recordUser({commit},userInfo){
    commit(RECEIVE_USER_INFO,userInfo)
  },
  //异步获取用户信息
  async getUserInfo({commit}){
    const res = await reqUserInfo()
    if (res.code == 0 ){
      const userInfo = res.data
      console.log(userInfo);
      commit(RECEIVE_USER_INFO,userInfo)
    }

  },
  //异步获取用户信息
  async logout({commit}){
    const res = await reqLogout()
    if (res.code === 0 ){
      commit(RESET_USER_INFO)
    }
  }
}
